package com.ruoyi.flowable.listener.move;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquMove;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 移动-7-1监听
 */
@Slf4j
@Component("moveListener_7_1")
public class MoveListener_7_1 implements ExecutionListener {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            EquMove equMove = execution.getVariable("equMove", EquMove.class);
            //执行信息
            Map<String, Object> executeInfoMap = objectMapper.readValue(equMove.getExecuteInfo(), new TypeReference<Map<String, Object>>() {});
            HashMap<String, Object> dataMap = new HashMap<>();
            String situation7_1 = execution.getVariable("situation7_1", String.class);
            dataMap.put("设备状态", situation7_1);
            String detailSituation7_1 = execution.getVariable("detailSituation7_1", String.class);
            dataMap.put("损伤描述", detailSituation7_1);
            String moveTime7_1 = execution.getVariable("moveTime7_1", String.class);
            dataMap.put("移动时间", moveTime7_1);
            String completeTime7_1 = execution.getVariable("completeTime7_1", String.class);
            dataMap.put("完成时间", completeTime7_1);
            executeInfoMap.put("移入科室验收", dataMap);
            //"借用"的移动时间在最后算
            if (!equMove.getMoveType().equals("临时借用")) {
                //计算实际移动时长(小时)
                BigDecimal actDuration = getActDuration(moveTime7_1, completeTime7_1);
                executeInfoMap.put("实际移动时长", actDuration);
            }
            executeInfoMap.put("实际移动时间", moveTime7_1);
            String executeInfo = objectMapper.writeValueAsString(executeInfoMap);
            //移动后状态:设备状态+损伤描述<如有>+新归属科室/存放地点
            HashMap<String, Object> moveAfterStatusMap = new HashMap<>();
            HashMap<String, Object> valueMap = new HashMap<>();
            valueMap.put("设备状态", situation7_1);
            valueMap.put("损伤描述", detailSituation7_1);
            valueMap.put("新归属科室", execution.getVariable("toDept", String.class));
            valueMap.put("存放地点", execution.getVariable("toLocation", String.class));
            moveAfterStatusMap.put("移入科室验收", valueMap);
            String moveAfterStatus = objectMapper.writeValueAsString(moveAfterStatusMap);

            equMove.setExecuteInfo(executeInfo);
            equMove.setMoveAfterStatus(moveAfterStatus);
            execution.setVariable("equMove", equMove);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    //获取现在到time的小时差
    public static BigDecimal getActDuration(String time1, String time2) {
        Duration duration = Duration.between(LocalDateTime.parse(time1, formatter), LocalDateTime.parse(time2, formatter));
        return BigDecimal.valueOf(Math.abs(duration.toMinutes()) / 60.0).setScale(1, RoundingMode.HALF_UP);
    }
}
